package cn.demoxy.controller;
import cn.demoxy.annotation.Log;
import cn.demoxy.component.CronTaskRegistrar;
import cn.demoxy.enums.BusinessTypeEnum;
import cn.demoxy.handler.SchedulingRunnable;
import cn.demoxy.result.R;
import cn.demoxy.service.IDemoJobService;
import cn.demoxy.model.dto.DemoJobDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;

import cn.demoxy.model.entity.DemoJob;

/**
* 任务表控制层
* @author demo
* @date 2022-08-26
*/
@Api(tags =" 任务表接口信息 ")
@AllArgsConstructor
@RestController
@RequestMapping("/admin/demo-job")
public class DemoJobController {

    private IDemoJobService demoJobService;

    @Autowired
    private CronTaskRegistrar cronTaskRegistrar;


    @PostMapping("/startTask")
    @Log(title = "任务管理任务启动",businessType = BusinessTypeEnum.JOB_START)
    public R startTask(@RequestBody  DemoJob entity){
        SchedulingRunnable task = new SchedulingRunnable(entity.getJobFile(), entity.getJobMethod(),entity.getJobParms());
        cronTaskRegistrar.addCronTask(task, entity.getJobCron());
        entity.setJobStatus(0);
        demoJobService.updateById(entity);
        return R.ok();
    }

    @PostMapping("/stopTask")
    @Log(title = "任务管理任务停止",businessType = BusinessTypeEnum.JOB_STOP)
    public R stopTask(@RequestBody  DemoJob entity){
        SchedulingRunnable task = new SchedulingRunnable(entity.getJobFile(), entity.getJobMethod(), entity.getJobParms());
        cronTaskRegistrar.removeCronTask(task);
        entity.setJobStatus(1);
        demoJobService.updateById(entity);
        return R.ok();
    }
    /**
    * 查询所有
    * @params
    * @return
    */
    @GetMapping("/list")
    @ApiOperation(value = "查询所有",notes = "查询所有")
    // @RequiresPermissions("demo-job:list")
    public R list() {
    return R.ok(this.demoJobService.list());
    }

    /**
    * 分页查询
    * @params dto
    * @return
    */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询",notes = "分页查询")
    // @RequiresPermissions("demo-job:page")
    public R page(@RequestBody  DemoJobDTO dto) {
    return R.ok(this.demoJobService.pageJob(dto));
    }

    /**
    * 通过id查询
    * @params id
    * @return
    */
    @GetMapping("/get")
    @ApiOperation(value = "通过id查询",notes = "通过id查询")
    // @RequiresPermissions("demo-job:get")
    public R get(String id) {
    return R.ok(this.demoJobService.getById(id));
    }

    /**
    * 添加
    * @params entity
    * @return
    */
    @ApiOperation(value = "添加",notes = "添加")
    @PostMapping("/save")
    @Log(title = "任务管理任务新增",businessType = BusinessTypeEnum.INSERT)
    // @RequiresPermissions("demo-job:save")
    public R save(@RequestBody  DemoJob entity) {
    return R.ok(this.demoJobService.save(entity));
    }

    /**
    * 更新
    * @params entity
    * @return
    */
    @PostMapping("/update")
    @ApiOperation(value = "更新",notes = "更新")
    @Log(title = "任务管理任务信息修改",businessType = BusinessTypeEnum.UPDATE)
    // @RequiresPermissions("demo-job:update")
    public R update(@RequestBody  DemoJob entity) {
    return R.ok(this.demoJobService.updateById(entity));
    }

    /**
    * 通过id删除
    * @params id
    * @return
    */
    @PostMapping("/delete/{id}")
    @ApiOperation(value = "通过id删除",notes = "通过id删除")
    @Log(title = "任务管理任务删除",businessType = BusinessTypeEnum.DELETE)
    // @RequiresPermissions("demo-job:delete")
    public R delete(@PathVariable("id")String id) {
    return R.ok(this.demoJobService.removeById(id));
    }
    }
